package com.travel.server.manager.impl.command.order;

import com.travel.common.CommonConstant;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.order.QueryOrderDetailRequest;
import com.travel.server.manager.dto.response.order.QueryOrderDetailResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.*;
import com.travel.server.manager.impl.enums.OrderStateEnum;
import com.travel.server.manager.impl.sal.CustomerSal;
import com.travel.server.manager.impl.sal.OrderSal;
import com.travel.server.manager.model.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/5 18:18
 * @Desc:
 */
@Component
@Slf4j
public class QueryOrderDetailCommand extends AbstractCommand<QueryOrderDetailRequest, QueryOrderDetailResponse> {

    @Autowired
    private OrderDal orderDal;

    @Autowired
    private ProjectDal projectDal;

    @Autowired
    private SpecialDal specialDal;

    @Autowired
    private CustomerDal customerDal;

    @Autowired
    private RefundDal refundDal;

    @Override
    protected void checkInput(QueryOrderDetailRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
    }

    @Override
    public QueryOrderDetailResponse handle(QueryOrderDetailRequest request) {
        checkInput(request);

        Integer id = request.getId();
        OrderInfo orderInfo = orderDal.selectById(id);

        Integer customerId = orderInfo.getCustomerId();
        CustomerInfo customerInfo = customerDal.selectById(customerId);

        Integer specialId = orderInfo.getSpecialId();
        ProjectSpecial special = specialDal.selectById(specialId);

        Integer projectId = special.getProjectId();
        ProjectInfo projectInfo = projectDal.selectById(projectId);

        OrderRefundInfo orderRefundInfo = null;
        if(OrderStateEnum.REFUND_ING.getState().equals(orderInfo.getOrderState()) || OrderStateEnum.REFUND_SUCCESS.getState().equals(orderInfo.getOrderState())){
            orderRefundInfo = refundDal.selectByOrderId(id);
        }

        return makeResponse(orderInfo, customerInfo, special, projectInfo, orderRefundInfo);
    }

    private QueryOrderDetailResponse makeResponse(OrderInfo orderInfo, CustomerInfo customerInfo, ProjectSpecial special, ProjectInfo projectInfo, OrderRefundInfo orderRefundInfo){
        QueryOrderDetailResponse response = new QueryOrderDetailResponse();
        response.setBuyer(customerInfo.getName());
        response.setBuyerBirth(orderInfo.getYouzanBirth());
        response.setBuyerMail(orderInfo.getYouzanMail());
        response.setBuyerMessage(orderInfo.getBuyerMessage());
        response.setBuyerMobile(orderInfo.getYouzanMobile());
        response.setBuyerName(orderInfo.getYouzanName());
        response.setBuyerSex(orderInfo.getYouzanSex());
        response.setBuyerWechat(orderInfo.getYouzanWechat());
        if(StringUtils.isNotBlank(orderInfo.getCollectAddress())){
            response.setCollectAddress(orderInfo.getCollectAddress());
        }else {
            response.setCollectAddress(CustomerSal.getCollectAddress(customerInfo));
        }
        response.setCollectMobile(StringUtils.isBlank(orderInfo.getCollectMobile()) ? customerInfo.getMobile() : orderInfo.getCollectMobile());
        response.setCollectName(StringUtils.isBlank(orderInfo.getCollectName()) ? customerInfo.getName() : orderInfo.getCollectName());
        response.setCommodityName(projectInfo.getProjectGroup() + CommonConstant.SPILT_STR + projectInfo.getName());
        response.setCompleteTime(orderInfo.getCompleteTime());
        response.setDeliveryTime(orderInfo.getDeliveryTime());
        if(orderInfo.getDiscountAmount() != null) {
            response.setDiscountAmount(CommonUtil.fenToYuan(orderInfo.getDiscountAmount()).toString());
        }
        response.setDistributeType(orderInfo.getDistributeType());
        response.setEmail(customerInfo.getMail());
        response.setIsTravelData(orderInfo.getIsTravelData());
        response.setIsYouzan(CommonConstant.YES.equals(orderInfo.getOrderMark()) ? CommonConstant.YES : CommonConstant.NO);
        if(orderInfo.getOrderAmount() != null) {
            response.setOrderAmount(CommonUtil.fenToYuan(orderInfo.getOrderAmount()).toString());
        }
        if(orderInfo.getFreightAmount() != null){
            response.setFreightAmount(CommonUtil.fenToYuan(orderInfo.getFreightAmount()).toString());
        }
        response.setOrderStars(orderInfo.getStars());
        response.setNeedEnglish(projectInfo.getNeedEnglish());
        response.setOrderId(orderInfo.getOrderId());
        response.setOrderState(orderInfo.getOrderState());
        response.setOrderTime(orderInfo.getOrderTime());
        response.setPayTime(orderInfo.getPayTime());
        response.setOrderTotal(orderInfo.getOrderTotal() + CommonConstant.EMPTY_STR);
        response.setOrderType(orderInfo.getOrderType());
        response.setTeamNumber(orderInfo.getTeamNumber());
        response.setPayType(orderInfo.getPayType());
        response.setProjectPicUrl(projectInfo.getProjectPicUrl());
        response.setSellerNote(orderInfo.getNote());
        response.setSpecialNum(special.getId() + CommonConstant.EMPTY_STR);
        response.setStep(orderInfo.getOrderStep());
        Long totalAmount = OrderSal.getTotalAmount(orderInfo);
        if(totalAmount != null){
            response.setTotalAmount(CommonUtil.fenToYuan(totalAmount).toString());
        }
        if(orderInfo.getTravelStartTime() != null && orderInfo.getTravelEndTime() != null) {
            response.setTravelDate(DateUtil.dateToStr(orderInfo.getTravelStartTime(), DateUtil.YYYY_MM_DD) + " - " + DateUtil.dateToStr(orderInfo.getTravelEndTime(), DateUtil.YYYY_MM_DD));
        }
        if(orderRefundInfo != null) {
            response.setRefundId(orderRefundInfo.getRefundId());
            response.setRefundAmount(CommonUtil.fenToYuan(orderRefundInfo.getRefundAmount()).toString());
            response.setRefundNote(orderRefundInfo.getRefundReason());
        }
        return response;
    }
}
